package maze.net.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import maze.contexts.ServerContext;

public class MazeService {

	private MazeService() {
		try (ServerSocket serverSocket = new ServerSocket(ServerContext.port)) {
			System.out.println("MazeService start: " + serverSocket);
			Socket waitingSocket = null;
			Socket socket = null;
			for (;;) {
				try {
					socket = serverSocket.accept();
					System.out.println("Accept socket: " + socket);
					if (waitingSocket == null) {
						waitingSocket = socket;
					} else {
						GameHouse house = new GameHouse(waitingSocket, socket);
						house.start();
						waitingSocket = null;
					}
				} catch (IOException e) {
					e.printStackTrace();
					if (socket != null)
						socket.close();
					if (waitingSocket != null)
						socket.close();
					continue;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new MazeService();
	}
}
